package com.myhome.controller;


import com.myhome.entity.Position;
import com.myhome.entity.RespBean;
import com.myhome.service.IPositionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-09-04
 */
@RestController
@RequestMapping("/system/basic/position")
public class PositionController {
    @Autowired
    IPositionService positionService;

    @ApiOperation(value = "获取全部职位信息")
    @GetMapping("/")
    public List<Position> getAllPosition() {
        List<Position> result = positionService.list();
        return result;
    }

    @ApiOperation(value = "增加职位信息")
    @PostMapping("/putPosition")
    public RespBean putPosition(@RequestBody Position position) {
        position.setCreateDate(LocalDateTime.now());
        if (positionService.save(position)) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "修改职位信息")
    @PutMapping("/update")
    public RespBean updatePosition(@RequestBody Position position) {
        if (positionService.updateById(position)) {
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }

    @ApiOperation(value = "删除职位信息")
    @DeleteMapping("/delete/{id}")
    public RespBean deletePosition(@PathVariable("id") Integer id) {
        if (positionService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "批量删除职位信息")
    @DeleteMapping("/deleteMore")
    public RespBean deleteMorePosition(Integer[] ids){
        if (positionService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除成功");
    }
}
